package com.ms.module.wechat.clear.fragment.scan;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ms.module.wechat.clear.R;

import com.ms.module.wechat.clear.base.BaseFragment;
import com.ms.module.wechat.clear.base.RxView;

import com.ms.module.wechat.clear.internal.WeChatClearCommonn;
import com.ms.module.wechat.clear.utils.ByteSizeToStringUnitUtils;
import com.ms.view.loading.FastChargeView;

import java.util.ArrayList;
import java.util.List;

public class ScanFragment extends BaseFragment implements RxView.Action1<View> {

    private static final String TAG = "WeChatClearScanIngActiv";

    private ScanFragmentViewModel viewModel;
    private TextView textViewTotalScanGarbageSize;
    private RecyclerView recyclerView;
    private WeChatScanIngRecyclerViewAdapter adapter;
    private List<WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem> datas = new ArrayList<>();

    private Observer<Long> observerTotalScanGarbageSize;
    private Observer<List<String>> observerRubbish;
    private Observer<List<String>> observerVideo;
    private Observer<List<String>> observerImage;
    private Observer<List<String>> observerVoice;
    private Observer<List<String>> observerReceiveFile;
    private Observer<List<String>> observerEmoji;


    private boolean rubbishFinish = false;
    private boolean mp4Finish = false;
    private boolean imageFinish = false;
    private boolean voiceFinish = false;
    private boolean receiveFileFinish = false;
    private boolean emojiFinish = false;

    private ImageView imageViewBack;
    private FastChargeView fastChargeView;
    private NavController navController;


    public static ScanFragment newInstance() {

        Bundle args = new Bundle();

        ScanFragment fragment = new ScanFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wechat_clear_scan_ing;
    }

    @Override
    protected void initView() {
        super.initView();
        textViewTotalScanGarbageSize = findViewById(R.id.textViewTotalScanGarbageSize);
        imageViewBack = findViewById(R.id.imageViewBack);
        fastChargeView = findViewById(R.id.fastChargeView);
        RxView.setOnClickListeners(this::onClick, imageViewBack);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        // 进入扫描页面
        WeChatClearCommonn.getWeChatClearCallBack().onEnterWeChatClearScan(getActivity(), Lifecycle.Event.ON_CREATE);
        navController = NavHostFragment.findNavController(ScanFragment.this);
        viewModel = new ViewModelProvider(this).get(ScanFragmentViewModel.class);
        viewModel.clearData();

        fastChargeView.setDuration(800);
        fastChargeView.startAnimation();

        observerTotalScanGarbageSize = new Observer<Long>() {
            @Override
            public void onChanged(Long size) {
                if (size != null) {
                    // 回调当前扫描的大小
                    WeChatClearCommonn.getWeChatClearCallBack().onCurrentGarbageSize(size);
                    textViewTotalScanGarbageSize.setText(ByteSizeToStringUnitUtils.byteToStringUnit(size) + "");
                }
            }
        };
        viewModel.getMutableLiveDataTotalScanGarbageSize().observe(getViewLifecycleOwner(), observerTotalScanGarbageSize);

        observerRubbish = new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {

                if (!rubbishFinish) {
                    rubbishFinish = true;
                    datas.get(0).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.FINISH;
                    viewModel.getMutableLiveDataVideo().postValue(null);
                    datas.get(1).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.ING;
                    adapter.notifyDataSetChanged();
                }
            }
        };

        viewModel.getLiveDataRubbish().observe(getViewLifecycleOwner(), observerRubbish);

        observerVideo = new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                if (!mp4Finish) {
                    mp4Finish = true;
                    datas.get(1).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.FINISH;
                    viewModel.getMutableLiveDataImage().postValue(null);
                    datas.get(2).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.ING;
                    adapter.notifyDataSetChanged();
                }
            }
        };

        viewModel.getLiveDataVideo().observe(getViewLifecycleOwner(), observerVideo);

        observerImage = new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                if (!imageFinish) {
                    imageFinish = true;
                    datas.get(2).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.FINISH;
                    viewModel.getMutableLiveDataVoice().postValue(null);
                    datas.get(3).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.ING;
                    adapter.notifyDataSetChanged();
                }
            }
        };

        viewModel.getLiveDataImage().observe(getViewLifecycleOwner(), observerImage);

        observerVoice = new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {

                if (!voiceFinish) {

                    voiceFinish = true;
                    datas.get(3).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.FINISH;
                    viewModel.getMutableLiveDataReceiveFile().postValue(null);
                    datas.get(4).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.ING;
                    adapter.notifyDataSetChanged();
                }


            }
        };

        viewModel.getLiveDataVoice().observe(getViewLifecycleOwner(), observerVoice);
        observerReceiveFile = new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {

                if (!receiveFileFinish) {

                    receiveFileFinish = true;
                    datas.get(4).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.FINISH;
                    viewModel.getMutableLiveDataEmoji().postValue(null);
                    datas.get(5).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.ING;
                    adapter.notifyDataSetChanged();
                }
            }
        };

        viewModel.getLiveDataReceiveFile().observe(getViewLifecycleOwner(), observerReceiveFile);


        observerEmoji = new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                if (!emojiFinish) {
                    emojiFinish = true;
                    datas.get(5).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.FINISH;
                    adapter.notifyDataSetChanged();
                    navController.navigate(R.id.clearFragment);
                }
            }
        };

        viewModel.getLiveDataEmoji().observe(getViewLifecycleOwner(), observerEmoji);


        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        datas.add(new WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem("垃圾文件", R.drawable.image_rubbish));
        datas.add(new WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem("聊天小视频", R.drawable.image_video));
        datas.add(new WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem("聊天图片", R.drawable.image_image));
        datas.add(new WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem("聊天语音", R.drawable.image_voice));
        datas.add(new WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem("文件", R.drawable.image_file));
        datas.add(new WeChatScanIngRecyclerViewAdapter.WeChatScanIngRecyclerViewAdapterItem("聊天表情包", R.drawable.image_emoji));

        adapter = new WeChatScanIngRecyclerViewAdapter(getActivity(), datas);
        recyclerView.setAdapter(adapter);

        datas.get(0).status = WeChatScanIngRecyclerViewAdapter.SCAN_STATUS.ING;
        viewModel.getMutableLiveDataRubbish().postValue(null);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onResume() {
        super.onResume();

        if (!viewModel.getMutableLiveDataTotalScanGarbageSize().hasActiveObservers()) {
            viewModel.getMutableLiveDataTotalScanGarbageSize().observe(this, observerTotalScanGarbageSize);
        }

        if (!rubbishFinish) {
            if (!viewModel.getLiveDataRubbish().hasActiveObservers()) {
                viewModel.getLiveDataRubbish().observe(this, observerRubbish);
            }
        }
        if (!mp4Finish) {

            if (!viewModel.getLiveDataVideo().hasActiveObservers()) {
                viewModel.getLiveDataVideo().observe(this, observerVideo);
            }
        }
        if (!imageFinish) {
            if (!viewModel.getLiveDataImage().hasActiveObservers()) {
                viewModel.getLiveDataImage().observe(this, observerImage);
            }
        }
        if (!voiceFinish) {
            if (!viewModel.getLiveDataVoice().hasActiveObservers()) {
                viewModel.getLiveDataVoice().observe(this, observerVoice);
            }
        }
        if (!receiveFileFinish) {
            if (!viewModel.getLiveDataReceiveFile().hasActiveObservers()) {
                viewModel.getLiveDataReceiveFile().observe(this, observerReceiveFile);
            }
        }
        if (!emojiFinish) {
            if (!viewModel.getLiveDataEmoji().hasActiveObservers()) {
                viewModel.getLiveDataEmoji().observe(this, observerEmoji);
            }
        }

    }


    @Override
    public void onStop() {
        super.onStop();

        if (viewModel.getLiveDataRubbish().hasObservers()) {
            viewModel.getLiveDataRubbish().removeObserver(observerRubbish);
        }
        if (viewModel.getLiveDataVideo().hasObservers()) {
            viewModel.getLiveDataVideo().removeObserver(observerVideo);
        }
        if (viewModel.getLiveDataImage().hasObservers()) {
            viewModel.getLiveDataImage().removeObserver(observerImage);
        }
        if (viewModel.getLiveDataVoice().hasObservers()) {
            viewModel.getLiveDataVoice().removeObserver(observerVoice);
        }
        if (viewModel.getLiveDataReceiveFile().hasObservers()) {
            viewModel.getLiveDataReceiveFile().removeObserver(observerReceiveFile);
        }
        if (viewModel.getLiveDataEmoji().hasObservers()) {
            viewModel.getLiveDataEmoji().removeObserver(observerEmoji);
        }
        if (viewModel.getMutableLiveDataTotalScanGarbageSize().hasActiveObservers()) {
            viewModel.getMutableLiveDataTotalScanGarbageSize().removeObserver(observerTotalScanGarbageSize);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.imageViewBack) {
            getActivity().finish();
            WeChatClearCommonn.getWeChatClearCallBack().onClose(getActivity());
        }
    }
}
